#include <stdio.h>
#include <mysql.h>

int main(int arg,char *argv[]){
	int i;
	MYSQL *conn;
	MYSQL_RES *result;
	MYSQL_ROW row;
	conn=mysql_init(NULL);
	if (mysql_real_connect(conn,"localhost","root","1234","jxgl",0,NULL,0)==NULL){
		fprintf(stderr, "no such a database\n");
	}
	mysql_query(conn,"set names 'utf8'");
	const char *sql ="select * from student;";
	if (mysql_query(conn,sql)){
		fprintf(stderr, "%s\n",mysql_error(conn) );
		fprintf(stderr, "%s\n", sql);
		return 1;
	}
		result=mysql_store_result(conn);
	if(result == NULL){
		if (mysql_error(conn))
			fprintf(stderr, "%s\n", mysql_error(conn));
		else
			fprintf(stderr, "%s\n", "unknownerror");
		return 1;
	}
	printf("%i\n",(int)mysql_num_rows(result) );
	while(row = mysql_fetch_row(result) != NULL){
		for (i=0;i< mysql_num_field(result);i++){
		if (row[i] == NULL )
				printf("[NULL]\n");
			else
				printf("%s\t",row[i] );
		}
		printf("\n");
		mysql_free_result(result);
		mysql_close(conn);
		return 0;
	}
}